package com.t.bricks.bussiness.service.nas.impl;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.t.bricks.bussiness.service.nas.NasService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.folder.FoldInfo;
import com.t.bricks.utils.function.folder.FoldUtil;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 网络附属存储 基本业务操作类接口定义
 */
@Service
public class NasServiceImpl implements NasService {

	/**
	 * 文件存储根路径
	 */
	@Value("${app.nas.file.path.root:}")
	private String nasPathRoot;
	
	/**
	 * 取文件存储根路径
	 * @return
	 */
	private String getNasPathRoot() {
		if (null == nasPathRoot || "".equals(nasPathRoot)) {
			nasPathRoot = String.format("%s/data/userdata/nas/", System.getProperty("user.dir").replaceAll("\\\\", "/"));
		}
		
		String userId = ModuleUtil.currentLoginUserId();
		
		String result = String.format("%s%s/", nasPathRoot, userId == null ? "guest" : userId);
		File file = new File(result);
		if (!file.exists()) {
			file.mkdirs();
		}
		
		return result;
	}
	
	/**
	 * 文件上传
	 * @param file
	 * @return
	 */
	@Override
	public MsgEmity upload(MultipartFile file) {
		return ModuleUtil.upload(file, getNasPathRoot());
	}
	
	/**
	 * 文件下载
	 * @param filePath
	 * @return
	 */
	@Override
	public MsgEmity download(String filePath) {
		if (null == filePath || "".equals(filePath.trim())) {
			return MsgEmity.err(8001, "请求参数为空");
		}
		
		filePath = filePath.trim();
		
		String nasPathRoot = getNasPathRoot();
		
		File file = new File(filePath);
		if (file.exists()) {
			if (file.getPath().indexOf(nasPathRoot) != 0) {
				return MsgEmity.err(8002, "请求越界!");
			}
		}
		
		filePath = String.format("%s%s", nasPathRoot, filePath).replaceAll("//", "/");
		
		file = new File(filePath);
		if (!file.exists()) {
			return MsgEmity.err(8003, "文件不存在!");
		}

		MsgEmity me = ModuleUtil.downFile(filePath, file.getName());
		if (!me.isSuccess()) {
			return me;
		}
		
		return null;//me不能返回,否则下载的文件中内容会有me
	}
	
	/**
	 * 指定目录下的文件列表
	 * @param dir 目录
	 * @param findAll 是否递归获取
	 * @return
	 */
	@Override
	public MsgEmity getlist(String dir, boolean findAll) {
		if (null == dir || "".equals(dir.trim())) {
			dir = "/";
		}
		
		dir = dir.trim();
		
		String nasPathRoot = getNasPathRoot();
		
		File file = new File(dir);
		if (file.exists() && !"/".equals(dir)) {
			if (file.getPath().indexOf(nasPathRoot) != 0) {
				return MsgEmity.err(8002, "请求越界!");
			}
		}
		
		dir = String.format("%s%s", nasPathRoot, dir).replaceAll("//", "/");
		
		file = new File(dir);
		if (!file.exists()) {
			return MsgEmity.err(8003, "目录不存在!");
		}
		
		List<FoldInfo> list = FoldUtil.getFileObjAndSelf(dir, findAll, nasPathRoot);
		
		return MsgEmity.success(list, "查找成功");
	}
	
}
